package com.example.healkep.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.healkep.R;
import com.example.healkep.bean.Food;
import com.huawei.hms.core.aidl.IMessageEntity;

import java.util.List;

public class FoodListAdapter extends RecyclerView.Adapter<FoodListAdapter.ViewHolder> {
    private List<Food> foods;
    private Context context;
    private OnItemClickListener listener;
    public FoodListAdapter(List<Food> foods, Context context, OnItemClickListener listener){
        this.foods =foods;
        this.context =context;
        this.listener = listener;
    }
    @SuppressLint("NotifyDataSetChanged")
    public void update(List<Food> newFoods){
        foods.clear();
        foods.addAll(newFoods);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.food_list_item_layout, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.imageView.setImageResource(R.drawable.test1);
        holder.tvTitle.setText(foods.get(position).getName());
        holder.tvContent.setText(foods.get(position).getIngredients());
        holder.layout.setOnClickListener((v)->{
            listener.ClickListener(foods.get(position));
        });
    }


    @Override
    public int getItemCount() {
        return foods.size();
    }
    public interface OnItemClickListener{
        void ClickListener(Food food);
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvTitle;
        TextView tvContent;
        RelativeLayout layout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_img);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            layout = itemView.findViewById(R.id.root);
        }
    }
}
